# frozen_string_literal: true

require_relative 'seeds/variable_type_seed'
require_relative 'seeds/schema_seed'
require_relative 'seeds/general_rule_seed'
require_relative 'seeds/decision_table_seed'

module Seeds
  def self.run
    drop_all

    init_data

    init_user
  end

  def self.init_data
    VariableTypeSeed.run
    SchemaSeed.run

    GeneralRuleSeed.run
    DecisionTableSeed.run
  end

  def self.drop_all
    # general_rule
    RuleCondition.delete_all
    RuleOutput.delete_all

    # decision_table
    DecisionTableCell.delete_all
    DecisionTableRow.delete_all
    DecisionTableHeader.delete_all

    # rule_set
    RuleSetRule.delete_all

    # rule_schema
    RuleSchema.delete_all

    # schema
    SchemaVariable.delete_all
    Schema.delete_all

    # rule
    RuleEntity.delete_all
    RuleVersion.delete_all
    Rule.delete_all

    TypeMethod.delete_all
    VariableType.delete_all
  end

  def self.init_user
    user = 'admin@example.com'
    password = 'password'
    User.find_by(email: user) || User.create!(email: user, password: password, password_confirmation: password)
  end
end

Seeds.run
